<?php

namespace App\Http\Controllers\Common;

use App\models\Upload;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
   public function make(Request $request,Upload $upload){
      $dir = 'upload/' . date('ymd');
      $fileName = auth()->id().'-'.str_random(10).time().'.'.$request->file->getClientOriginalExtension();
      $file = $request->file->move($dir,$fileName);
//      在上传数据表中记录文件
       if (auth()->id())
       {
           $user = auth()->id();
        }elseif(auth('admin')->id())
       {
           $user = auth('admin')->id();

       }
       $upload['user_id'] = $user;
       $upload['path'] = url($file);
       $upload['url']=url($file);
       $upload['name']=$request->file->getClientOriginalName();
       $upload->save();
       return ['file' => url($file), 'code' => 0];
   }
   public function lists(){
       $uploads = Upload::paginate(10);
       return [
           'data' => $uploads->toArray()['data'],
           'page' => $uploads->links() .'',
           'code' => 0,
       ];
   }

    public function layuiUpload(Request $request, Upload $upload){
        $dir = 'upload/' . date('ym');
        $fileName = auth()->id().'-'.str_random(10) . time() . '.' . $request->file->getClientOriginalExtension();
        $file = $request->file->move($dir, $fileName);

        //往上传数据表中记录文件
        if (auth()->id())
        {
            $user = auth()->id();
        }elseif(auth('admin')->id())
        {
            $user = auth('admin')->id();

        }
        $upload['user_id'] = $user;
        $upload['path'] =url( $file);
        $upload['url'] = url($file);
        $upload['name'] = $request->file->getClientOriginalName();
        $upload->save();

//        return ['file' => url($file), 'code' => 0];
        return ['code' => 0, 'msg' => '','data'=>[
            'src'=>url($file),
        ]];
    }
}
